package com.example.changeactivity03_2;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ActMain extends Activity {

	//2013-12-25 呼叫一個新的ActMain顯示在畫面上
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actmain);
		InitialComponet();
	}

	private void InitialComponet() {
		// TODO Auto-generated method stub
		btn = (Button)super.findViewById(R.id.btn);
		btn.setOnClickListener(btn_Ok);
		
		sendBtn = (Button)super.findViewById(R.id.btnSend);
		sendBtn.setOnClickListener(send_btn);
		
		receive = (Button)super.findViewById(R.id.btnReceive);
		receive.setOnClickListener(receive_btn);
		
		message =(TextView)super.findViewById(R.id.messageGet);
		
		btnList=(Button)super.findViewById(R.id.btnList);
		btnList.setOnClickListener(btnList_click);
	}
	
	//這邊要接收點選後的傳值
	OnClickListener btnList_click=new OnClickListener(){
		public void onClick(View arg0) {
			Intent intent =new Intent(ActMain.this,ActList.class);
			startActivityForResult(intent, 1);	
			
	}};
	
	
	//接收三個參數,識別碼,結果選項,Intent物件
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(data==null){
			message.setText("沒有管理員");
			return;
		}
		if(data.getExtras()==null){
			message.setText("沒有包裹");
			return;
		}
		//識別碼
		if(requestCode==689){
			//如果確認689,把data包裹打開,看內容
			String strData = data.getExtras().getString("KK");
			message.setText(strData);
		}
		else{
			String strData = data.getExtras().getString("GG");
			message.setText(strData);
		}
	
	}


	OnClickListener receive_btn = new OnClickListener() {
		public void onClick(View v) {
			Intent intent = new Intent(ActMain.this,Act4.class);
			//我們指定傳回來資料的識別碼是689
			startActivityForResult(intent, 689);
		}
	};
	
	OnClickListener send_btn = new OnClickListener() {
		public void onClick(View v) {
			//傳送資料給Act3,先包成Bundle後在解壓包裹
			Bundle bund = new Bundle();
			bund.putString("QK", "Hello");
			Intent intent = new Intent(ActMain.this,Act3.class);
			//把包裹交給管理員
			intent.putExtras(bund);
			startActivity(intent);		
		}
	};

	OnClickListener btn_Ok = new OnClickListener() {
		public void onClick(View v) {		
			//new一個Intent去呼叫新的Act2
			Intent intent = new Intent(ActMain.this,Act2.class);
			//去啟動新的Intent Activity
			startActivity(intent);		
		}
	};
	
	
	Button btn = null;
	Button sendBtn = null;
	Button receive = null;
	TextView message = null;
	Button btnList=null;
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.act_main, menu);
		return true;
	}
	

}
